package com.mec.web.y2012.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class RefreshModelListServlet
 */
@WebServlet("/ajax/refreshModelList")
public class RefreshModelListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static List<MakeModelYear> availableModels = new ArrayList<MakeModelYear>();
	
	private static class MakeModelYear{
		private int modelYear;
		private String make;
		private String model;
		
		public MakeModelYear(int modelYear, String make, String model){
			this.modelYear = modelYear;
			this.make = make;
			this.model = model;
			
		}
	}
	
	public void init() throws ServletException{
		availableModels.add(new MakeModelYear(2006, "Dodge", "Charger"));
		availableModels.add(new MakeModelYear(2006, "Dodge", "Magnum"));
		availableModels.add(new MakeModelYear(2006, "Dodge", "Rum"));
		availableModels.add(new MakeModelYear(2006, "Dodge", "Viper"));
		
		availableModels.add(new MakeModelYear(1995, "Dodge", "Avenger"));
		availableModels.add(new MakeModelYear(1995, "Dodge", "Intrepid"));
		availableModels.add(new MakeModelYear(1995, "Dodge", "Neon"));
		availableModels.add(new MakeModelYear(1995, "Dodge", "Spirit"));
		
		availableModels.add(new MakeModelYear(1985, "Dodge", "Aries"));
		availableModels.add(new MakeModelYear(1985, "Dodge", "Daytona"));
		availableModels.add(new MakeModelYear(1985, "Dodge", "Diplomat"));
		availableModels.add(new MakeModelYear(1985, "Dodge", "Omni"));
		
		availableModels.add(new MakeModelYear(1970, "Dodge", "Challenger"));
		availableModels.add(new MakeModelYear(1970, "Dodge", "Charger"));
		availableModels.add(new MakeModelYear(1970, "Dodge", "Coronet"));
		availableModels.add(new MakeModelYear(1970, "Dodge", "Dart"));
		
		availableModels.add(new MakeModelYear(2006, "Chevrolet", "Colorado"));
		availableModels.add(new MakeModelYear(2006, "Chevrolet", "Corvette"));
		availableModels.add(new MakeModelYear(2006, "Chevrolet", "Equinox"));
		availableModels.add(new MakeModelYear(2006, "Chevrolet", "Monte Carlo"));
		
		availableModels.add(new MakeModelYear(1995, "Chevrolet", "Beretta"));
		availableModels.add(new MakeModelYear(1995, "Chevrolet", "Camaro"));
		availableModels.add(new MakeModelYear(1995, "Chevrolet", "Cavalier"));
		availableModels.add(new MakeModelYear(1995, "Chevrolet", "Lumina"));
		availableModels.add(new MakeModelYear(1985, "Chevrolet", "Cavalier"));
		availableModels.add(new MakeModelYear(1985, "Chevrolet", "Chevette"));
		availableModels.add(new MakeModelYear(1985, "Chevrolet", "Celebrity"));
		availableModels.add(new MakeModelYear(1985, "Chevrolet", "Citation II"));
		availableModels.add(new MakeModelYear(1970, "Chevrolet", "Bel Air"));
		availableModels.add(new MakeModelYear(1970, "Chevrolet", "Caprice"));
		availableModels.add(new MakeModelYear(1970, "Chevrolet", "Chevelle"));
		availableModels.add(new MakeModelYear(1970, "Chevrolet", "Monte Carlo"));
		
		availableModels.add(new MakeModelYear(2006, "Pontiac", "G6"));
		availableModels.add(new MakeModelYear(2006, "Pontiac", "Grand Prix"));
		availableModels.add(new MakeModelYear(2006, "Pontiac", "Solstice"));
		availableModels.add(new MakeModelYear(2006, "Pontiac", "Vibe"));
		availableModels.add(new MakeModelYear(1995, "Pontiac", "Bonneville"));
		availableModels.add(new MakeModelYear(1995, "Pontiac", "Grand Am"));
		availableModels.add(new MakeModelYear(1995, "Pontiac", "Grand Prix"));
		availableModels.add(new MakeModelYear(1995, "Pontiac", "Firebird"));
		availableModels.add(new MakeModelYear(1985, "Pontiac", "6000"));
		availableModels.add(new MakeModelYear(1985, "Pontiac", "Fiero"));
		availableModels.add(new MakeModelYear(1985, "Pontiac", "Grand Prix"));
		availableModels.add(new MakeModelYear(1985, "Pontiac", "Parisienne"));
		availableModels.add(new MakeModelYear(1970, "Pontiac", "Catalina"));
		availableModels.add(new MakeModelYear(1970, "Pontiac", "GTO"));
		availableModels.add(new MakeModelYear(1970, "Pontiac", "LeMans"));
		availableModels.add(new MakeModelYear(1970, "Pontiac", "Tempest"));
	}
    public RefreshModelListServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//response.setContentType("text/html;charset=UTF-8");
		
		int modelYear = Integer.parseInt(request.getParameter("modelYear"));
		String make = request.getParameter("make");
		
		//
		StringBuffer results = new StringBuffer("<models>");
		//MakeModelYear availableModel = null;
		
		for(MakeModelYear availableModel :availableModels){
			if(modelYear == availableModel.modelYear
					&&
				make.equals(availableModel.make)
					){
				results.append("<model>"+ availableModel.model + "</model>");
			}
		}
		results.append("</models>");
		
		
		//
		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		out.write(results.toString());
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	private static final Logger logger = 
			Logger.getLogger(RefreshModelListServlet.class);

}
